﻿
namespace PortableRiotGames.Infrastructure.Converters
{
    using Newtonsoft.Json;
    using Newtonsoft.Json.Linq;
    using System;
    using System.Collections.Generic;
    using System.Reflection;

    public abstract class JsonCustomConverter<T> : JsonConverter
    {
        protected abstract T Create(Type objectType, JContainer container);

        public override bool CanConvert(Type objectType)
        {
            return typeof(T).GetTypeInfo().IsAssignableFrom(objectType.GetTypeInfo());
        }

        public override object ReadJson(JsonReader reader, Type objectType, object existingValue, JsonSerializer serializer)
        {
            try
            {
                JContainer container = null;
                if (reader.TokenType == JsonToken.StartArray)
                {
                    container = JArray.Load(reader);
                }
                else
                {
                    container = JObject.Load(reader);
                }
                var target = Create(objectType, container);
                serializer.Populate(container.CreateReader(), target);
                return target;
            }
            catch (Exception ex)
            {
                throw ex;
            }
        }

        public override void WriteJson(JsonWriter writer, object value, JsonSerializer serializer)
        {
            throw new NotImplementedException();
        }
    }
}
